import menus from "@/assets/data/menus.js"
import router from "@/router"

// 面包屑的格式化封装
export const mapbreadcrumb = (path: string) => {
    const newBreadcumbs: any[] = []
    menus?.forEach((item: any) => {
        const childern = item?.children?.find((menu: any) => menu.path === path)
        if (childern) {
            newBreadcumbs.push(childern)
            newBreadcumbs.push({ name: item.name, path: item.children[0].path })
        }
    })
    return newBreadcumbs
}

// 注册全部的动态路由
export const mapMenuRouter = () => {
    const files: Record<string, any> = import.meta.glob("../router/home/**/*.ts", { eager: true })

    for (const key in files) {
        const route = files[key]?.default
        if (route) router.addRoute('home', route)
    }

}

// 根据url来匹配我们的active
export const mapRouteActiveFn = (path: string) => {
    let newAction = '1'
    menus?.forEach((item: any) => {
        const childern = item?.children?.find((menu: any) => menu.path === path)
        if (childern) {
            newAction = childern.sort
        }
    })

    return String(newAction)
}
